/*
 * @Autor: xgq
 * @Description: 
 * @Version: 1.0
 * @Date: 2020-05-12 10:04:53
 */

var express = require('express');
var banner_router = express.Router();
var db = require('../../models');
var path = require('path')
var comm = require('../../comm')

/**
 * @author: xgq
 * @description: 加载banner列表
 * @param {type} 
 * @return: 
 */
banner_router.post('/list',async (req,res)=>{
  var [page = 1, pagesize = 20, where = null] = [req.body.page, req.body.pagesize, req.body.where]
  let condition = {
    "pagesize": pagesize,
    "page": page
  }
  if(where!==null){
    if(where.position!==''&&where.position!==null&&where.position!==undefined&&where.position>0){
      condition.position=where.position;
    }
  }
    let bannerList = await db.banner.listBanner(condition)
    bannerList.code = 200
    res.json(bannerList)
})

/**
 * @author: xgq
 * @description: 新增顶部Banner
 * @param {type} 
 * @return: 
 */
banner_router.post('/addBanner',async (req,res)=>{
    let { title, thumbnail} = req.body
    /**
     * 移动thumbnail 到images文件夹下
     */
    thumbnail = thumbnail.replace('temp_files/', '')
    var newthumbnail= `banner/${thumbnail}`
    var sourceFile = path.join(process.cwd(), 'static/temp_files', thumbnail);
    var targetPath = path.join(process.cwd(), 'static/images',newthumbnail);
    await comm.move_image(sourceFile,targetPath)
    let result = await db.banner.addBanner({
      'title': title,
      'thumbnail':newthumbnail
    })
    res.json(result)
})

/**
 * @author: xgq
 * @description: 更新顶部banner信息
 * @param {type} 
 * @return: 
 */
banner_router.post('/updateBanner',async (req,res)=>{
    let {title,thumbnail,id} = req.body
    let banner=await db.banner.getBannerByid(id)
    if(banner.thumbnail!==thumbnail){
      comm.delete_imgage( path.join(process.cwd(), "static/images/", banner.thumbnail)  )
      var sourceFile = path.join(process.cwd(), 'static', thumbnail);
      thumbnail = thumbnail.replace('temp_files/', 'banner/')
      var targetPath = path.join(process.cwd(), 'static/images',thumbnail);
      await comm.move_image(sourceFile,targetPath)      
    }
    let result = await db.banner.updateBanner({
      'id': id,
      'title': title,
      'thumbnail':thumbnail
    })
    res.json(result)
})
/**
 * @author: xgq
 * @description: 更新底部banner信息
 * @param {type} 
 * @return: 
 */
banner_router.post('/updateFootBanner',async (req,res)=>{
  let from = req.body
  let thumbnail = from.thumbnail
  from.thumbnail = thumbnail.replace('temp_files/', 'banner/')

  let banner=await db.banner.getBannerByid(from.id)
  if(banner.thumbnail!==thumbnail){
    comm.delete_imgage( path.join(process.cwd(), "static/images/", banner.thumbnail)  )
    var sourceFile = path.join(process.cwd(), 'static', thumbnail);
    var targetPath = path.join(process.cwd(), 'static/images', from.thumbnail);
    await comm.move_image(sourceFile,targetPath)      
  }

  let result = await db.banner.updateFootBanner(from)
  res.json(result)
})

/**
 * @author: xgq
 * @description: 新增底部banner
 * @param {type} 
 * @return: 
 */
banner_router.post('/addFootBanner',async (req,res)=>{
  let { thumbnail } = req.body
  thumbnail = thumbnail.replace('temp_files/', '')
  var newthumbnail= `banner/${thumbnail}`
  var sourceFile = path.join(process.cwd(), 'static/temp_files', thumbnail);
  var targetPath = path.join(process.cwd(), 'static/images',newthumbnail);
  let moveed = await comm.move_image(sourceFile,targetPath)

  if( moveed !== 'error' ){
    var form = req.body
    form.thumbnail = newthumbnail
    let result = await db.banner.addFootBanner(form)
    res.json(result)
  }else{
    res.json(false)
  }    
})
/**
 * @author: xgq
 * @description: 关闭
 * @param {type} 
 * @return: 
 */
banner_router.get('/bannerOff',async (req,res)=>{
    let result=await db.banner.bannerOff(req.query.id)
    res.json(result)
})
/**
 * @author: xgq
 * @description: 开启
 * @param {type} 
 * @return: 
 */
banner_router.get('/bannerUp',async (req,res)=>{
    let result=await db.banner.bannerUp(req.query.id)
      res.json(result)
})
/**
 * @author: xgq
 * @description: 拖动排序
 * @param {type} 
 * @return: 
 */
banner_router.post('/bannerSort',async (req,res)=>{
    //start拖动的目的地
      //end从哪一行开始拖动的
      //start《end从后往前拖动   start》end从前往后拖动
      let result=await db.banner.bannerSort(req.body.params)
      res.json(result)
})


banner_router.get('/checkTitle',async (req,res)=>{
    let result=await db.banner.checkTitle(req.query.title)
      res.json(result)
})


banner_router.post('/deleteBanner',async (req,res)=>{
	let result = { 'code': 500 }  
  let { sort,id} = req.body
  let banner=await db.banner.getBannerByid(id)
  if( banner == null ){
    return res.json(result)
  }
  comm.delete_imgage( path.join(process.cwd(), "static/images/", banner.thumbnail))
  await db.banner.deleteBanner({
    'id': id,
    'sort': sort
  }).then(() => {
    result.code=200
    res.json(result)
  }) .catch(() => {
    res.json(result)
  });
})

module.exports = banner_router
